<template>
  <el-form ref="registerForm" :model="registerForm" :rules="rules">
    <el-form-item label="" prop="username">
      <el-input
        v-model="registerForm.username"
        type="text"
        autocomplete="off"
        prefix-icon="el-icon-user-solid"
        placeholder="请输入身份证号码"
      />
    </el-form-item>
    <el-form-item label="" prop="nickname">
      <el-input
        v-model="registerForm.nickname"
        type="text"
        autocomplete="off"
        prefix-icon="el-icon-user-solid"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item label="" prop="password1">
      <el-input
        v-model="registerForm.password1"
        type="password"
        autocomplete="off"
        prefix-icon="el-icon-lock"
        placeholder="请输入密码"
      />
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input
        v-model="registerForm.password"
        type="password"
        autocomplete="off"
        prefix-icon="el-icon-lock"
        placeholder="请确认密码"
      />
    </el-form-item>
    <el-form-item class="btns">
      <el-button type="primary" @click="submitForm">
        注册
      </el-button>
      <el-button @click="resetForm('registerForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { register } from '@/api/select/user'

export default {
  name: 'Register',
  data() {
    var validatePwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerForm.password !== '') {
          this.$refs.registerForm.validateField('password')
        }
        callback()
      }
    }

    var validatePwd2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password1) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'register',
      registerForm: {
        username: '',
        nickname: '',
        password1: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入您的身份证号码', trigger: 'blur' },
          { min: 0, max: 18, message: '长度在 0 到 18 个字符', trigger: 'blur' }
        ],
        password1: [{ required: true, validator: validatePwd, trigger: 'blur' }],
        password: [
          { required: true, validator: validatePwd2, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    submitForm() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          register(this.registerForm).then(response => {
            console.log(response)
            this.$message({
              type: 'success',
              message: '注册成功'
            })
            this.registerForm = ''
          }).catch(error => {
            console.log(error)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    resetForm(formName) {
      this.$refs.registerForm.resetFields()
    }
  }
}
</script>
<style scoped>
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
